using System;
using System.Web;
using THANHTUNGO.Portal.Components.Settings;
using THANHTUNGO.Portal.Components.UI;

namespace THANHTUNGO.Portal.Modules.MODULE
{
    public partial class ModuleSetting : EditItemPage
    {
        private Module GetModule()
        {
            // Obtain PortalSettings for this tab
            PortalSettings portalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];

            // Obtain selected module data
            foreach (Module _module in portalSettings.ActiveTab.Modules)
            {
                if (_module.ID == this.ModuleID)
                    return _module;
            }
            return null;
        }

        protected override void OnInit(EventArgs e)
        {
            ButtonUpdate = this.btnApply;
            ButtonCancel = this.btnCancel;

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                object value = GetModule();
                if (value != null)
                {
                    Module m = (Module) value;
                    moduleTitle.Text = m.Title;
                    cacheTime.Text = m.CacheTimeOut.ToString();
                    chkTitle.Checked = m.ShowTitle;
                }
            }
        }

        protected override void OnUpdate(EventArgs e)
        {
            base.OnUpdate(e);
            object value = GetModule();
            if (value != null)
            {
                Module m = (Module) value;
                
                Configuration config = new Configuration();
                config.UpdateModule(this.ModuleID, m.Order, m.PaneName, chkTitle.Checked, moduleTitle.Text, Int32.Parse(cacheTime.Text));

                moduleTitle.Text = m.Title;
                cacheTime.Text = m.CacheTimeOut.ToString();
            }
            // Redirect back to the portal home page
            ReturnToReferrerPage();
        }
    }
}